package com.tiancheng.trade.order.controller.app;

import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.commom.core.exception.error.BusinessErrorInfoEnum;
import com.tiancheng.trade.order.service.BankCardService;
import com.tiancheng.trade.order.util.SecurityUtils;
import com.tiancheng.trade.order.util.ums.UmsBindingBankCardConfig;
import com.tiancheng.trade.order.vo.app.BankCardBindingVo;
import com.tiancheng.trade.order.vo.app.SetCardVO;
import com.tiancheng.trade.order.vo.app.UnboundCardVO;
import com.tiancheng.trade.order.vo.app.res.BankCardBindingResVo;
import com.tiancheng.trade.order.vo.app.res.BankCardVO;
import com.tiancheng.trade.order.vo.app.res.BankInfoVo;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: likailun
 * @Description: 绑卡操作，客户端调用
 * @Date: create in 2021/8/23 4:01 下午
 */
@Slf4j
@RestController
@RequestMapping("/app/bankCard")
@Validated
public class BankCardAppController {
    @Resource
    private BankCardService bankCardService;
    @Resource
    private UmsBindingBankCardConfig umsConfig;

    @GetMapping(value = "/queryBankList")
    public Result<Map<String, Object>> queryBankList() {
        final Map<String, Object> result = new HashMap<>();
        final List<BankInfoVo> bankInfoVos = bankCardService.queryBankList();
        result.put("bank_list",bankInfoVos);
        result.put("agreements",umsConfig.getAgreements());
        return Result.success(result);
    }

    /**
     * 获取已开通的银行卡列表
     *
     * @param uid
     * @return
     */
    @GetMapping(value = "/queryCardList")
    public Result queryByUid(@RequestParam(value = "uid",defaultValue = "") String uid) {
        if(StringUtils.isEmpty(uid)){
            uid = SecurityUtils.getLoginUserId();
        }
        BusinessErrorInfoEnum.ORDER_USER_NOT_AUTH.assertIsTrue(StringUtils.isNotEmpty(uid));
        return Result.success(bankCardService.queryByUid(uid));
    }

    /**
     * 获取绑卡信息
     *
     * @param uid 用户id
     * @param flowId 开卡单号
     * @return
     */
    @GetMapping(value = "/queryBindingStatusByFlowId")
    public Result<BankCardVO> queryBindingStatus(@RequestParam(value = "uid",defaultValue = "")  String uid, @RequestParam("flow_id") @NotEmpty String flowId) {
        if(StringUtils.isEmpty(uid)){
            uid = SecurityUtils.getLoginUserId();
        }
        BusinessErrorInfoEnum.ORDER_USER_NOT_AUTH.assertIsTrue(StringUtils.isNotEmpty(uid));
        return Result.success(bankCardService.queryBindingStatus(uid,flowId));
    }

    /**
     * 申请绑卡
     *
     * @param vo
     * @return
     */
    @PostMapping(value = "/binding", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result<BankCardBindingResVo> binding(@Valid @RequestBody BankCardBindingVo vo, HttpServletRequest request) {
        if(StringUtils.isEmpty(vo.getUid())){
            vo.setUid(SecurityUtils.getLoginUserId());
        }
        BusinessErrorInfoEnum.ORDER_USER_NOT_AUTH.assertIsTrue(StringUtils.isNotEmpty(vo.getUid()));
        log.info("用户申请绑卡，uid:{}",vo.getUid());
        return Result.success(bankCardService.binding(vo));
    }

    /**
     * 申请绑卡
     *
     * @param setCardVO
     * @return
     */
    @PostMapping(value = "/unbound", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result unbound(@Valid @RequestBody SetCardVO setCardVO) {
        if(StringUtils.isEmpty(setCardVO.getUid())){
            setCardVO.setUid(SecurityUtils.getLoginUserId());
        }
        BusinessErrorInfoEnum.ORDER_USER_NOT_AUTH.assertIsTrue(StringUtils.isNotEmpty(setCardVO.getUid()));
        bankCardService.unbound(setCardVO);
        return Result.success();
    }

    /**
     * 解绑所有银行卡
     *
     * @param unboundCardVO
     * @return
     */
    @PostMapping(value = "/unboundAllBankCard", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result unboundAll(@RequestBody(required=false) UnboundCardVO unboundCardVO) {
        if(null==unboundCardVO){
            unboundCardVO=new UnboundCardVO();
        }
        if(StringUtils.isEmpty(unboundCardVO.getUid())){
            unboundCardVO.setUid(SecurityUtils.getLoginUserId());
        }
        BusinessErrorInfoEnum.ORDER_USER_NOT_AUTH.assertIsTrue(StringUtils.isNotEmpty(unboundCardVO.getUid()));
        bankCardService.unboundAllBankCard(unboundCardVO);
        return Result.success();
    }

    /**
     * 解绑银行卡+一码通
     *
     * @param unboundCardVO
     * @return
     */
    @PostMapping(value = "/unboundAllBankCardAndOneCode", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result unboundAllBankCardAndOneCode(@RequestBody(required=false) UnboundCardVO unboundCardVO) {
        if(null==unboundCardVO){
            unboundCardVO=new UnboundCardVO();
        }
        if(StringUtils.isEmpty(unboundCardVO.getUid())){
            unboundCardVO.setUid(SecurityUtils.getLoginUserId());
        }
        BusinessErrorInfoEnum.ORDER_USER_NOT_AUTH.assertIsTrue(StringUtils.isNotEmpty(unboundCardVO.getUid()));
        bankCardService.unboundAllBankCardAndOneCode(unboundCardVO);
        return Result.success();
    }


    /**
     * 设置为默认支付卡
     *
     * @param setCardVO 设置默认卡参数uid、flow_id
     * @return
     */
    @PostMapping(value = "/setDefaultPayCard", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result setDefaultPayCard(@Valid @RequestBody SetCardVO setCardVO) {
        if(setCardVO!=null &&StringUtils.isEmpty(setCardVO.getUid())){
            setCardVO.setUid(SecurityUtils.getLoginUserId());
        }
        bankCardService.setDefaultPayCard(setCardVO);
        return Result.success();
    }

}
